package cn.edu.xmu.oomall.payment.dao;

import cn.edu.xmu.oomall.core.util.ReturnNo;
import cn.edu.xmu.oomall.core.util.ReturnObject;
import cn.edu.xmu.oomall.payment.mapper.PaymentPoMapper;
import cn.edu.xmu.oomall.payment.mapper.RefundPoMapper;
import cn.edu.xmu.oomall.payment.model.bo.Payment;
import cn.edu.xmu.oomall.payment.model.bo.PaymentPattern;
import cn.edu.xmu.oomall.payment.model.bo.Refund;
import cn.edu.xmu.oomall.payment.model.po.PaymentPo;
import cn.edu.xmu.oomall.payment.model.po.PaymentPoExample;
import cn.edu.xmu.oomall.payment.model.po.RefundPo;
import cn.edu.xmu.oomall.payment.model.po.RefundPoExample;
import cn.edu.xmu.privilegegateway.annotation.util.Common;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class RefundDao {
    @Autowired
    private RefundPoMapper refundPoMapper;

    /**
     * 增加退款单
     */
    public ReturnObject insertRefund(Refund refund){
        RefundPo refundPo= Common.cloneVo(refund,RefundPo.class);
        try{
            int ret=refundPoMapper.insert(refundPo);
            if(ret==0){
                return new ReturnObject(ReturnNo.FIELD_NOTVALID);
            }
            return new ReturnObject(refundPo);
        }catch (Exception e){
            return new ReturnObject(ReturnNo.INTERNAL_SERVER_ERR);
        }
    }

    /**
     * 修改
     */
    public ReturnObject updateRefund(Refund refund){
        RefundPo refundPo=Common.cloneVo(refund,RefundPo.class);
        try{
            int ret=refundPoMapper.updateByPrimaryKey(refundPo);
            if(ret==0){
                return new ReturnObject(ReturnNo.FIELD_NOTVALID);
            }
            return new ReturnObject(ReturnNo.OK);
        }catch (Exception e){
            return new ReturnObject(ReturnNo.INTERNAL_SERVER_ERR);
        }
    }

    /**
     * 删除退款单
     */
    public ReturnObject deleteRefund(Long id){
        try{
            int ret=refundPoMapper.deleteByPrimaryKey(id);
            if(ret==0){
                return new ReturnObject(ReturnNo.FIELD_NOTVALID);
            }
            return new ReturnObject(ReturnNo.OK);
        } catch (Exception e){
            return new ReturnObject(ReturnNo.INTERNAL_SERVER_ERR);
        }
    }

    /**
     * 根据时间间隔查看退款单
     */
    public ReturnObject selectRefundByTime(LocalDateTime beginTime, LocalDateTime endTime){
        RefundPoExample example=new RefundPoExample();
        RefundPoExample.Criteria criteria=example.createCriteria();
        criteria.andRefundTimeBetween(beginTime,endTime);
        try{
            List<RefundPo> refundPos=refundPoMapper.selectByExample(example);
            List<Refund> refunds=new ArrayList<>(refundPos.size());
            for(RefundPo refundPo:refundPos){
                Refund refund=Common.cloneVo(refundPo,Refund.class);
                refunds.add(refund);
            }
            return new ReturnObject(refunds);
        } catch (Exception e){
            return new ReturnObject(ReturnNo.INTERNAL_SERVER_ERR);
        }
    }

    /**
     * 根据退款交易流水号查看退款单
     */
    public ReturnObject selectRefundByTradeSn(String tradeSn){
        RefundPoExample example=new RefundPoExample();
        RefundPoExample.Criteria criteria=example.createCriteria();
        criteria.andTradeSnEqualTo(tradeSn);
        try{
            List<RefundPo> refundPos=refundPoMapper.selectByExample(example);
            RefundPo po=refundPos.get(0);
            return new ReturnObject(Common.cloneVo(po,Refund.class));
        } catch (Exception e){
            return new ReturnObject(ReturnNo.INTERNAL_SERVER_ERR);
        }
    }

    /**
     * 根据id查看退款单
     */
    public ReturnObject selectRefundById(Long id){
        try{
            RefundPo refundPo=refundPoMapper.selectByPrimaryKey(id);
            return new ReturnObject(Common.cloneVo(refundPo,Refund.class));
        } catch (Exception e){
            return new ReturnObject(ReturnNo.INTERNAL_SERVER_ERR);
        }
    }


    /**
     * 获取退款单的所有状态
     * @return ReturnObject
     */
    public ReturnObject getAllRefundStates() {
        try {
            List<Map<String, Object>> stateList = new ArrayList<>();
            for (Refund.State states : Refund.State.values()) {
                Map<String, Object> temp = new HashMap<>();
                temp.put("code", states.getCode());
                temp.put("name", states.getDescription());
                stateList.add(temp);
            }
            return new ReturnObject<>(stateList);
        }
        catch (Exception e) {
            return new ReturnObject<>(ReturnNo.INTERNAL_SERVER_ERR);
        }
    }
}
